#include <QDebug>
#include <QSettings>
#include "ethThread.h"
#include "KgGlobalData.h"

EthThread::EthThread(QObject *parent, CRTDB *pRTDB, int bcIndex, int ethIndex, int devType): QThread(parent),m_pRTDB(pRTDB),m_bcIndex(bcIndex),m_ethIndex(ethIndex),m_devType(devType)
{
    devConfig(m_devType);  
    KG_LOG_INFO(QString("VER1.0 RTDB%1.%2.%3").arg(BAMS_VERSION_MAJOR).arg(BAMS_VERSION_MINOR).arg(BAMS_VERSION_REVISION));
    m_bOpen = 0;
    m_tcpSocket = 0;
    m_pBattStack = m_pRTDB->getStack(0);
}

EthThread::~EthThread()
{}

void EthThread::run()
{
    while(1)
    {
        if (!m_bOpen)
        {
            m_bOpen = tcpInit(m_serverIP, m_serverPort);
            msleep(200);
        }
        msleep(10);
        switch(m_devType)
        {
            case 0: readBcmuData(m_bcIndex); break;
            case 1: readPcsData(m_bcIndex); break;
            case 2: readUpsData(m_bcIndex); break;
            case 3: readBmsData(m_bcIndex); break;
        }
    }
}

void EthThread::readPcsData(int index)
{
    CPcsData *pPcsData = m_pRTDB->getPcs(index);
    QSettings set(DEV_PATH, QSettings::IniFormat);
    for (int i=0; i<95; i++) 
    {
        pPcsData->m_uRev[i] = set.value(QString("PCS-%1/m_uRev[%2]").arg(index+1).arg(i), pPcsData->m_uRev[i]).toInt();
    }
    for (int i=0; i<98; i++) 
    {
        pPcsData->m_fRev[i] = set.value(QString("PCS-%1/m_fRev[%2]").arg(index+1).arg(i), pPcsData->m_fRev[i]).toFloat();
    }            
    switch(dev.m_uFunc)
    {
        case 2030: funcPcs2030(dev, pPcsData, index); break; //【首航】【EBI系列储能变流器PCS与EMS之间的Modbus外部通讯协议V1.7】
    }
    m_bOpen = !dev.m_uCommState;
    if (dev.m_uCommLastState != dev.m_uCommState)
    {
        if ((dev.m_Time.secsTo(QDateTime::currentDateTime()) >= dev.m_uCommTimeout) || (dev.m_uCommState == 0))
        {
            dev.m_Time = QDateTime::currentDateTime();
            dev.m_uCommLastState = dev.m_uCommState;
            pPcsData->m_uCommState = dev.m_uCommState;
            QString str = QString("连接逆变器%1 EMS通信%2").arg(index+1).arg(dev.m_uCommState?"失败":"成功");
            KG_LOG_INFO(str);
            saveLogToBaDb(str);  
        }    
    }
    else
    {
        dev.m_Time = QDateTime::currentDateTime();
    }
}

void EthThread::funcPcs2030(devData &dev, CPcsData *pPcsData, int index) //【首航】【EBI系列储能变流器PCS与EMS之间的Modbus外部通讯协议V1.7】
{
    QSettings set(DEVTEMP_PATH, QSettings::IniFormat);
    switch(set.value(QString("PCS/Strategy"), 0).toUInt())
    {
        case 1: {
            if (pPcsData->m_uReset == 1)
            {
                pPcsData->m_uReset = 0;
                // pPcsData->;
            }
            if (pPcsData->m_uStart == 1)
            {
                pPcsData->m_uStart = 0;
                pPcsData->m_fRev[26] = 1;
            }
            if (pPcsData->m_uStop == 1)
            {
                pPcsData->m_uStop = 0;
                pPcsData->m_fRev[26] = 0;
            }
            if (pPcsData->m_uStandby == 1)
            {
                pPcsData->m_uStandby = 0;
                pPcsData->m_fRev[26] = 0; // 无待机命令,发停机
            }
            if (pPcsData->m_fSetYgPower != FLT_MAX)
            {
                if (pPcsData->m_fRev[0] != FLT_MAX)
                {
                    tempf = (-pPcsData->m_fSetYgPower)/pPcsData->m_fRev[0]*100;
                    if (tempf > 125) tempf = 125;
                    else if (tempf < -125) tempf = -125;
                }
                else tempf = 0;
                pPcsData->m_fRev[31] = tempf;
                pPcsData->m_fSetYgPower = FLT_MAX;
            }
            if (pPcsData->m_fSetWgPower != FLT_MAX)
            {
                if (pPcsData->m_fRev[0] != FLT_MAX)
                {
                    tempf = (-pPcsData->m_fSetWgPower)/pPcsData->m_fRev[0]*100;
                    if (tempf > 100) tempf = 100;
                    else if (tempf < -100) tempf = -100;
                }
                else tempf = 0;
                pPcsData->m_fRev[30] = tempf;
                pPcsData->m_fSetWgPower = FLT_MAX;
            }
            CBattBank *pBattBank = m_pRTDB->getBank(index);
            tempf = pBattBank->fBankAllowMaxDischargePower/pPcsData->m_fRev[0]*100;
            if (tempf > 110) tempf = 110;
            else if (tempf < -110) tempf = -110;
            pPcsData->m_fRev[28] = tempf;
            tempf = pBattBank->fBankAllowMaxChargePower/pPcsData->m_fRev[0]*100;
            if (tempf > 110) tempf = 110;
            else if (tempf < -110) tempf = -110;
            pPcsData->m_fRev[29] = tempf;
        }
        break;
    }  
    do
    {
        ret = readRegData(dev.m_uAddress, 0x03, 40023, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40023 输出额定功率 0.1
            getFromBuff(m_pRecv + 9 + 2*(40023 - 40023), temp16);
            pPcsData->m_fRev[0] = (float)temp16/10;
            KG_LOG_INFO(QString("逆变器%1 输出额定功率=%2").arg(index+1).arg(pPcsData->m_fRev[0]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40100, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40100 内舱温度
            getFromBuff(m_pRecv + 9 + 2*(40100 - 40100), temp16);
            pPcsData->m_fRev[1] = (signed short)temp16;
            KG_LOG_INFO(QString("逆变器%1 内舱温度=%2").arg(index+1).arg(pPcsData->m_fRev[1]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40130, 5);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40130 功率因数 0.001
            getFromBuff(m_pRecv + 9 + 2*(40130 - 40130), temp16);
            pPcsData->m_fRev[2] = (float)((signed short)temp16)/1000;
            // 40131 电网频率 0.01
            getFromBuff(m_pRecv + 9 + 2*(40131 - 40130), temp16);
            pPcsData->m_fRev[3] = (float)((signed short)temp16)/100;
            // 40134 直流母线电压 0.1
            getFromBuff(m_pRecv + 9 + 2*(40134 - 40130), temp16);
            pPcsData->m_fRev[4] = (float)((signed short)temp16)/10;
            QString str = QString(" 功率因数=%1").arg(pPcsData->m_fRev[2]);
            str += QString(" 电网频率=%1").arg(pPcsData->m_fRev[3]);
            str += QString(" 直流母线电压=%1").arg(pPcsData->m_fRev[4]);
            KG_LOG_INFO(QString("逆变器%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40151, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40151 绝缘阻抗
            getFromBuff(m_pRecv + 9 + 2*(40151 - 40151), temp16);
            pPcsData->m_fRev[5] = temp16;
            KG_LOG_INFO(QString("逆变器%1 绝缘阻抗=%2").arg(index+1).arg(pPcsData->m_fRev[5]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40168, 8);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40168 当日电池充电量 0.01
            getFromBuff(m_pRecv + 9 + 2*(40168 - 40168), temp32);
            temp32 = temp32>>16|temp32<<16;
            pPcsData->m_fRev[6] = (float)temp32/100;
            // 40170 总电池充电量 0.1
            getFromBuff(m_pRecv + 9 + 2*(40170 - 40168), temp32);
            temp32 = temp32>>16|temp32<<16;
            pPcsData->m_fRev[7] = (float)temp32/10;
            // 40172 当日电池放电量 0.01
            getFromBuff(m_pRecv + 9 + 2*(40172 - 40168), temp32);
            temp32 = temp32>>16|temp32<<16;
            pPcsData->m_fRev[8] = (float)temp32/100;
            // 40174 总电池放电量 0.1
            getFromBuff(m_pRecv + 9 + 2*(40174 - 40168), temp32);
            temp32 = temp32>>16|temp32<<16;
            pPcsData->m_fRev[9] = (float)temp32/10;
            QString str = QString(" 当日电池充电量=%1").arg(pPcsData->m_fRev[6]);
            str += QString(" 总电池充电量=%1").arg(pPcsData->m_fRev[7]);
            str += QString(" 当日电池放电量=%1").arg(pPcsData->m_fRev[8]);
            str += QString(" 总电池放电量=%1").arg(pPcsData->m_fRev[9]);
            KG_LOG_INFO(QString("逆变器%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40230, 3);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40230 总有功功率 0.1
            getFromBuff(m_pRecv + 9 + 2*(40230 - 40230), temp16);
            pPcsData->m_fRev[10] = (float)((signed short)temp16)/10;
            // 40231 总无功功率 0.01
            getFromBuff(m_pRecv + 9 + 2*(40231 - 40230), temp16);
            pPcsData->m_fRev[11] = (float)((signed short)temp16)/100;
            // 40232 总视在功率 0.1
            getFromBuff(m_pRecv + 9 + 2*(40232 - 40230), temp16);
            pPcsData->m_fRev[12] = (float)((signed short)temp16)/10;
            QString str = QString(" 总有功功率=%1").arg(pPcsData->m_fRev[10]);
            str += QString(" 总无功功率=%1").arg(pPcsData->m_fRev[11]);
            str += QString(" 总视在功率=%1").arg(pPcsData->m_fRev[12]);
            KG_LOG_INFO(QString("逆变器%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40341, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40341 R相输出电流 0.01
            getFromBuff(m_pRecv + 9 + 2*(40341 - 40341), temp16);
            pPcsData->m_fRev[13] = (float)((signed short)temp16)/100;
            KG_LOG_INFO(QString("逆变器%1 R相输出电流=%2").arg(index+1).arg(pPcsData->m_fRev[13]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40451, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40451 S相输出电流 0.01
            getFromBuff(m_pRecv + 9 + 2*(40451 - 40451), temp16);
            pPcsData->m_fRev[14] = (float)((signed short)temp16)/100;
            KG_LOG_INFO(QString("逆变器%1 S相输出电流=%2").arg(index+1).arg(pPcsData->m_fRev[14]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40561, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40561 T相输出电流 0.01
            getFromBuff(m_pRecv + 9 + 2*(40561 - 40561), temp16);
            pPcsData->m_fRev[15] = (float)((signed short)temp16)/100;
            KG_LOG_INFO(QString("逆变器%1 T相输出电流=%2").arg(index+1).arg(pPcsData->m_fRev[15]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40671, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40671 RS线电压 0.1
            getFromBuff(m_pRecv + 9 + 2*(40671 - 40671), temp16);
            pPcsData->m_fRev[16] = (float)temp16/10;
            KG_LOG_INFO(QString("逆变器%1 RS线电压=%2").arg(index+1).arg(pPcsData->m_fRev[16]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40771, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40771 ST线电压 0.1
            getFromBuff(m_pRecv + 9 + 2*(40771 - 40771), temp16);
            pPcsData->m_fRev[17] = (float)temp16/10;
            KG_LOG_INFO(QString("逆变器%1 ST线电压=%2").arg(index+1).arg(pPcsData->m_fRev[17]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 40871, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 40871 TR线电压 0.1
            getFromBuff(m_pRecv + 9 + 2*(40871 - 40871), temp16);
            pPcsData->m_fRev[18] = (float)temp16/10;
            KG_LOG_INFO(QString("逆变器%1 TR线电压=%2").arg(index+1).arg(pPcsData->m_fRev[18]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 41455, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 41455 直流侧电流 0.1
            getFromBuff(m_pRecv + 9 + 2*(41455 - 41455), temp16);
            pPcsData->m_fRev[19] = (float)((signed short)temp16)/10;
            KG_LOG_INFO(QString("逆变器%1 直流侧电流=%2").arg(index+1).arg(pPcsData->m_fRev[19]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 41602, 10);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 41602 故障状态字1
            getFromBuff(m_pRecv + 9 + 2*(41602 - 41602), temp16);
            pPcsData->m_uRev[0] = temp16;
            // 41603 故障状态字2
            getFromBuff(m_pRecv + 9 + 2*(41603 - 41602), temp16);
            pPcsData->m_uRev[1] = temp16;
            // 41604 故障状态字3
            getFromBuff(m_pRecv + 9 + 2*(41604 - 41602), temp16);
            pPcsData->m_uRev[2] = temp16;
            // 41605 故障状态字4
            getFromBuff(m_pRecv + 9 + 2*(41605 - 41602), temp16);
            pPcsData->m_uRev[3] = temp16;
            // 41606 故障状态字5
            getFromBuff(m_pRecv + 9 + 2*(41606 - 41602), temp16);
            pPcsData->m_uRev[4] = temp16;
            // 41607 故障状态字6
            getFromBuff(m_pRecv + 9 + 2*(41607 - 41602), temp16);
            pPcsData->m_uRev[5] = temp16;
            // 41608 故障状态字7
            getFromBuff(m_pRecv + 9 + 2*(41608 - 41602), temp16);
            pPcsData->m_uRev[6] = temp16;
            // 41609 故障状态字8
            getFromBuff(m_pRecv + 9 + 2*(41609 - 41602), temp16);
            pPcsData->m_uRev[7] = temp16;
            // 41610 故障状态字9
            getFromBuff(m_pRecv + 9 + 2*(41610 - 41602), temp16);
            pPcsData->m_uRev[8] = temp16;
            // 41611 故障状态字10
            getFromBuff(m_pRecv + 9 + 2*(41611 - 41602), temp16);
            pPcsData->m_uRev[9] = temp16;
            QString str = QString(" 故障状态字1=%1").arg(pPcsData->m_uRev[0]);
            str += QString(" 故障状态字2=%1").arg(pPcsData->m_uRev[1]);
            str += QString(" 故障状态字3=%1").arg(pPcsData->m_uRev[2]);
            str += QString(" 故障状态字4=%1").arg(pPcsData->m_uRev[3]);
            str += QString(" 故障状态字5=%1").arg(pPcsData->m_uRev[4]);
            str += QString(" 故障状态字6=%1").arg(pPcsData->m_uRev[5]);
            str += QString(" 故障状态字7=%1").arg(pPcsData->m_uRev[6]);
            str += QString(" 故障状态字8=%1").arg(pPcsData->m_uRev[7]);
            str += QString(" 故障状态字9=%1").arg(pPcsData->m_uRev[8]);
            str += QString(" 故障状态字10=%1").arg(pPcsData->m_uRev[9]);
            KG_LOG_INFO(QString("逆变器%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 41645, 2);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 41645 告警状态字1
            getFromBuff(m_pRecv + 9 + 2*(41645 - 41645), temp16);
            pPcsData->m_uRev[10] = temp16;
            // 41646 告警状态字2
            getFromBuff(m_pRecv + 9 + 2*(41646 - 41645), temp16);
            pPcsData->m_uRev[11] = temp16;
            QString str = QString(" 告警状态字1=%1").arg(pPcsData->m_uRev[10]);
            str += QString(" 告警状态字2=%1").arg(pPcsData->m_uRev[11]);
            KG_LOG_INFO(QString("逆变器%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 41670, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 41670 运行状态
            getFromBuff(m_pRecv + 9 + 2*(41670 - 41670), temp16);
            pPcsData->m_uRev[12] = temp16;
            KG_LOG_INFO(QString("逆变器%1 运行状态=%2").arg(index+1).arg(pPcsData->m_uRev[12]));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 41800, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 41800 远程开关机
            getFromBuff(m_pRecv + 9 + 2*(41800 - 41800), temp16);
            pPcsData->m_fRev[20] = temp16;
            KG_LOG_INFO(QString("逆变器%1 远程开关机=%2").arg(index+1).arg(pPcsData->m_fRev[20]));
            msleep(dev.m_uDelayMs);
            if (pPcsData->m_fRev[26] != FLT_MAX)
            {
                if (pPcsData->m_fRev[26] != pPcsData->m_fRev[20])
                {
                    temp16 = (signed short)pPcsData->m_fRev[26];
                    ret = setSingleRegData(dev.m_uAddress, 0x06, 41800, temp16);
                    if (ret)
                    {
                        dev.m_uCommState = 0;
                        KG_LOG_INFO(QString("逆变器%1 下发远程开关机=%2").arg(index+1).arg(pPcsData->m_fRev[26]));
                        msleep(dev.m_uDelayMs);
                    }
                    else
                    {
                        dev.m_uCommState = 1;
                        break;
                    }
                }
            }
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 41830, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 41830 运行模式设置
            getFromBuff(m_pRecv + 9 + 2*(41830 - 41830), temp16);
            pPcsData->m_fRev[21] = temp16;
            KG_LOG_INFO(QString("逆变器%1 运行模式设置=%2").arg(index+1).arg(pPcsData->m_fRev[21]));
            msleep(dev.m_uDelayMs);
            if (pPcsData->m_fRev[27] != FLT_MAX)
            {
                if (pPcsData->m_fRev[27] != pPcsData->m_fRev[21])
                {
                    temp16 = (signed short)pPcsData->m_fRev[27];
                    ret = setSingleRegData(dev.m_uAddress, 0x06, 41830, temp16);
                    if (ret)
                    {
                        dev.m_uCommState = 0;
                        KG_LOG_INFO(QString("逆变器%1 下发运行模式设置=%2").arg(index+1).arg(pPcsData->m_fRev[27]));
                        msleep(dev.m_uDelayMs);
                    }
                    else
                    {
                        dev.m_uCommState = 1;
                        break;
                    }
                }
            }
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 41904, 3);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 41904 放电功率上限值 0.1
            getFromBuff(m_pRecv + 9 + 2*(41904 - 41904), temp16);
            pPcsData->m_fRev[22] = (float)((signed short)temp16)/10;
            // 41905 充电功率上限值 0.1
            getFromBuff(m_pRecv + 9 + 2*(41905 - 41904), temp16);
            pPcsData->m_fRev[23] = (float)((signed short)temp16)/10;
            // 41906 无功功率百分比 0.1
            getFromBuff(m_pRecv + 9 + 2*(41906 - 41904), temp16);
            pPcsData->m_fRev[24] = (float)((signed short)temp16)/10;
            QString str = QString(" 放电功率上限值=%1").arg(pPcsData->m_fRev[22]);
            str += QString(" 充电功率上限值=%1").arg(pPcsData->m_fRev[23]);
            str += QString(" 无功功率百分比=%1").arg(pPcsData->m_fRev[24]);
            KG_LOG_INFO(QString("逆变器%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
            if (pPcsData->m_fRev[28] != FLT_MAX)
            {
                if (pPcsData->m_fRev[28] != pPcsData->m_fRev[22])
                {
                    temp16 = (signed short)(pPcsData->m_fRev[28]*10);
                    ret = setSingleRegData(dev.m_uAddress, 0x06, 41904, temp16);
                    if (ret)
                    {
                        dev.m_uCommState = 0;
                        KG_LOG_INFO(QString("逆变器%1 下发放电功率上限值=%2").arg(index+1).arg(pPcsData->m_fRev[28]));
                        msleep(dev.m_uDelayMs);
                    }
                    else
                    {
                        dev.m_uCommState = 1;
                        break;
                    }
                }
            }
            if (pPcsData->m_fRev[29] != FLT_MAX)
            {
                if (pPcsData->m_fRev[29] != pPcsData->m_fRev[23])
                {
                    temp16 = (signed short)(pPcsData->m_fRev[29]*10);
                    ret = setSingleRegData(dev.m_uAddress, 0x06, 41905, temp16);
                    if (ret)
                    {
                        dev.m_uCommState = 0;
                        KG_LOG_INFO(QString("逆变器%1 下发充电功率上限值=%2").arg(index+1).arg(pPcsData->m_fRev[29]));
                        msleep(dev.m_uDelayMs);
                    }
                    else
                    {
                        dev.m_uCommState = 1;
                        break;
                    }
                }
            }
            if (pPcsData->m_fRev[30] != FLT_MAX)
            {
                if (pPcsData->m_fRev[30] != pPcsData->m_fRev[24])
                {
                    temp16 = (signed short)(pPcsData->m_fRev[30]*10);
                    ret = setSingleRegData(dev.m_uAddress, 0x06, 41906, temp16);
                    if (ret)
                    {
                        dev.m_uCommState = 0;
                        KG_LOG_INFO(QString("逆变器%1 下发无功功率百分比=%2").arg(index+1).arg(pPcsData->m_fRev[30]));
                        msleep(dev.m_uDelayMs);
                    }
                    else
                    {
                        dev.m_uCommState = 1;
                        break;
                    }
                }
            }
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x03, 41913, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 41913 充放电功率指令给定值 0.1
            getFromBuff(m_pRecv + 9 + 2*(41913 - 41913), temp16);
            pPcsData->m_fRev[25] = (float)((signed short)temp16)/10;
            KG_LOG_INFO(QString("逆变器%1 充放电功率指令给定值=%2").arg(index+1).arg(pPcsData->m_fRev[25]));
            msleep(dev.m_uDelayMs);
            if (pPcsData->m_fRev[31] != FLT_MAX)
            {
                if (pPcsData->m_fRev[31] != pPcsData->m_fRev[25])
                {
                    temp16 = (signed short)(pPcsData->m_fRev[31]*10);
                    ret = setSingleRegData(dev.m_uAddress, 0x06, 41913, temp16);
                    if (ret)
                    {
                        dev.m_uCommState = 0;
                        KG_LOG_INFO(QString("逆变器%1 下发充放电功率指令给定值=%2").arg(index+1).arg(pPcsData->m_fRev[31]));
                        msleep(dev.m_uDelayMs);
                    }
                    else
                    {
                        dev.m_uCommState = 1;
                        break;
                    }
                }
            }
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
    }while(0);
    if (!pPcsData->m_uCommState)
    {
        pPcsData->m_fABLineVol = pPcsData->m_fRev[16];
        pPcsData->m_fBCLineVol = pPcsData->m_fRev[17];
        pPcsData->m_fCALineVol = pPcsData->m_fRev[18];
        // pPcsData->m_fAPhaseVol = pPcsData->;
        // pPcsData->m_fBPhaseVol = pPcsData->;
        // pPcsData->m_fCPhaseVol = pPcsData->;
        pPcsData->m_fAPhaseCur = pPcsData->m_fRev[13];
        pPcsData->m_fBPhaseCur = pPcsData->m_fRev[14];
        pPcsData->m_fCPhaseCur = pPcsData->m_fRev[15];
        // pPcsData->m_fAPhaseActPower = pPcsData->;
        // pPcsData->m_fBPhaseActPower = pPcsData->;
        // pPcsData->m_fCPhaseActPower = pPcsData->;
        // pPcsData->m_fAPhaseReactPower = pPcsData->;
        // pPcsData->m_fBPhaseReactPower = pPcsData->;
        // pPcsData->m_fCPhaseReactPower = pPcsData->;
        // pPcsData->m_fAPhaseApprPower = pPcsData->;
        // pPcsData->m_fBPhaseApprPower = pPcsData->;
        // pPcsData->m_fCPhaseApprPower = pPcsData->;
        // pPcsData->m_fAPhasePowerFactor = pPcsData->;
        // pPcsData->m_fBPhasePowerFactor = pPcsData->;
        // pPcsData->m_fCPhasePowerFactor = pPcsData->;
        pPcsData->m_fTotalActPower = pPcsData->m_fRev[10];
        pPcsData->m_fTotalReactPower = pPcsData->m_fRev[11];
        pPcsData->m_fTotalApprPower = pPcsData->m_fRev[12];
        pPcsData->m_fTotalPowerFactor = pPcsData->m_fRev[2];
        pPcsData->m_fTotalFreq = pPcsData->m_fRev[3];
        pPcsData->m_fDirectVol = pPcsData->m_fRev[4];
        pPcsData->m_fDirectCur = pPcsData->m_fRev[19];
        // pPcsData->m_fDirectActPower = pPcsData->;
        pPcsData->m_fDeviceTemp = pPcsData->m_fRev[1];
        // 故障状态 1故障
        pPcsData->m_uFaultState = (pPcsData->m_uRev[12]==2)?1:0;
        // 停机状态 1停机
        pPcsData->m_uStopState = (pPcsData->m_uRev[12]==0)?1:0;
        // 运行状态 0运行
        pPcsData->m_uRunState = ((pPcsData->m_uRev[12]==3)||(pPcsData->m_uRev[12]==4)||(pPcsData->m_uRev[12]==5))?0:1;
        // pPcsData->m_uOnOffGridState = pPcsData->;
        // pPcsData->m_uWordHd = pPcsData->;
        // pPcsData->m_uWordHdSec = pPcsData->;
        // pPcsData->m_uWordNet = pPcsData->;
        // pPcsData->m_uWordMother = pPcsData->;
        // pPcsData->m_uWordAcCap = pPcsData->;
        // pPcsData->m_uWordSys = pPcsData->;
        // pPcsData->m_uWordSwitch = pPcsData->;
        // pPcsData->m_uWordOther = pPcsData->;
    }
}

void EthThread::readUpsData(int index)
{
    CUpsData *pUpsData = m_pRTDB->getUpsDev(index);
    QSettings set(DEV_PATH, QSettings::IniFormat);
    for (int i=0; i<99; i++) 
    {
        pUpsData->m_uRev[i] = set.value(QString("UPS-%1/m_uRev[%2]").arg(index+1).arg(i), pUpsData->m_uRev[i]).toInt();
    }
    for (int i=0; i<100; i++) 
    {
        pUpsData->m_fRev[i] = set.value(QString("UPS-%1/m_fRev[%2]").arg(index+1).arg(i), pUpsData->m_fRev[i]).toFloat();
    }            
    switch(dev.m_uFunc)
    {
        case 2030: funcUps2030(dev, pUpsData, index); break; //【MODBUS_UPS_C_v1.0】
    }
    m_bOpen = !dev.m_uCommState;
    if (dev.m_uCommLastState != dev.m_uCommState)
    {
        if ((dev.m_Time.secsTo(QDateTime::currentDateTime()) >= dev.m_uCommTimeout) || (dev.m_uCommState == 0))
        {
            dev.m_Time = QDateTime::currentDateTime();
            dev.m_uCommLastState = dev.m_uCommState;
            pUpsData->m_uCommState = dev.m_uCommState;
            QString str = QString("连接UPS%1 通信%2").arg(index+1).arg(dev.m_uCommState?"失败":"成功");
            KG_LOG_INFO(str);
            saveLogToBaDb(str);  
        }    
    }
    else
    {
        dev.m_Time = QDateTime::currentDateTime();
    } 
}

void EthThread::funcUps2030(devData &dev, CUpsData *pUpsData, int index) //【MODBUS_UPS_C_v1.0】
{
    do
    {
        ret = readRegData(dev.m_uAddress, 0x04, 1, 13);
        if (ret)
        {
            dev.m_uCommState = 0;
            for (int i=0; i<11; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);
                pUpsData->m_uRev[2 + i] = temp16;
            }
            getFromBuff(m_pRecv + 9 + 2*(13 - 1), temp16);
            pUpsData->m_uRev[13] = temp16;
            QString str = QString(" 供电方式=%1").arg(pUpsData->m_uRev[2]);
            str += QString(" 电池状态=%1").arg(pUpsData->m_uRev[3]);
            str += QString(" 机柜告警状态综合1=%1").arg(pUpsData->m_uRev[4]);
            str += QString(" 电池自检状态=%1").arg(pUpsData->m_uRev[5]);
            str += QString(" 电池手动自检开始=%1").arg(pUpsData->m_uRev[6]);
            str += QString(" 电池维护状态=%1").arg(pUpsData->m_uRev[7]);
            str += QString(" 机柜告警状态综合2=%1").arg(pUpsData->m_uRev[8]);
            str += QString(" 机柜告警状态综合3=%1").arg(pUpsData->m_uRev[9]);
            str += QString(" 机柜告警状态综合4=%1").arg(pUpsData->m_uRev[10]);
            str += QString(" 整流器状态=%1").arg(pUpsData->m_uRev[11]);
            str += QString(" 逆变器状态=%1").arg(pUpsData->m_uRev[12]);
            str += QString(" 旁路可供电状态=%1").arg(pUpsData->m_uRev[13]);
            KG_LOG_INFO(QString("UPS%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x04, 21, 14);
        if (ret)
        {
            dev.m_uCommState = 0;
            for (int i=0; i<14; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);
                pUpsData->m_uRev[14 + i] = temp16;
            }
            QString str = QString(" 机柜灯状态=%1").arg(pUpsData->m_uRev[14]);
            str += QString(" 综合告警=%1").arg(pUpsData->m_uRev[15]);
            str += QString(" 模块在线标识高位=%1").arg(pUpsData->m_uRev[16]);
            str += QString(" 模块在线标识低位=%1").arg(pUpsData->m_uRev[17]);
            str += QString(" 模块1灯状态=%1").arg(pUpsData->m_uRev[18]);
            str += QString(" 模块2灯状态=%1").arg(pUpsData->m_uRev[19]);
            str += QString(" 模块3灯状态=%1").arg(pUpsData->m_uRev[20]);
            str += QString(" 模块4灯状态=%1").arg(pUpsData->m_uRev[21]);
            str += QString(" 模块5灯状态=%1").arg(pUpsData->m_uRev[22]);
            str += QString(" 模块6灯状态=%1").arg(pUpsData->m_uRev[23]);
            str += QString(" 模块7灯状态=%1").arg(pUpsData->m_uRev[24]);
            str += QString(" 模块8灯状态=%1").arg(pUpsData->m_uRev[25]);
            str += QString(" 模块9灯状态=%1").arg(pUpsData->m_uRev[26]);
            str += QString(" 模块10灯状态=%1").arg(pUpsData->m_uRev[27]);
            KG_LOG_INFO(QString("UPS%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x04, 201, 40);
        if (ret)
        {
            dev.m_uCommState = 0;
            for (int i=0; i<40; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);
                pUpsData->m_uRev[28 + i] = temp16;
            }
            QString str = QString(" 模块1综合告警1=%1").arg(pUpsData->m_uRev[28]);
            str += QString(" 模块1综合告警2=%1").arg(pUpsData->m_uRev[29]);
            str += QString(" 模块1综合告警3=%1").arg(pUpsData->m_uRev[30]);
            str += QString(" 模块1综合告警4=%1").arg(pUpsData->m_uRev[31]);
            str += QString(" 模块2综合告警1=%1").arg(pUpsData->m_uRev[32]);
            str += QString(" 模块2综合告警2=%1").arg(pUpsData->m_uRev[33]);
            str += QString(" 模块2综合告警3=%1").arg(pUpsData->m_uRev[34]);
            str += QString(" 模块2综合告警4=%1").arg(pUpsData->m_uRev[35]);
            str += QString(" 模块3综合告警1=%1").arg(pUpsData->m_uRev[36]);
            str += QString(" 模块3综合告警2=%1").arg(pUpsData->m_uRev[37]);
            str += QString(" 模块3综合告警3=%1").arg(pUpsData->m_uRev[38]);
            str += QString(" 模块3综合告警4=%1").arg(pUpsData->m_uRev[39]);
            str += QString(" 模块4综合告警1=%1").arg(pUpsData->m_uRev[40]);
            str += QString(" 模块4综合告警2=%1").arg(pUpsData->m_uRev[41]);
            str += QString(" 模块4综合告警3=%1").arg(pUpsData->m_uRev[42]);
            str += QString(" 模块4综合告警4=%1").arg(pUpsData->m_uRev[43]);
            str += QString(" 模块5综合告警1=%1").arg(pUpsData->m_uRev[44]);
            str += QString(" 模块5综合告警2=%1").arg(pUpsData->m_uRev[45]);
            str += QString(" 模块5综合告警3=%1").arg(pUpsData->m_uRev[46]);
            str += QString(" 模块5综合告警4=%1").arg(pUpsData->m_uRev[47]);
            str += QString(" 模块6综合告警1=%1").arg(pUpsData->m_uRev[48]);
            str += QString(" 模块6综合告警2=%1").arg(pUpsData->m_uRev[49]);
            str += QString(" 模块6综合告警3=%1").arg(pUpsData->m_uRev[50]);
            str += QString(" 模块6综合告警4=%1").arg(pUpsData->m_uRev[51]);
            str += QString(" 模块7综合告警1=%1").arg(pUpsData->m_uRev[52]);
            str += QString(" 模块7综合告警2=%1").arg(pUpsData->m_uRev[53]);
            str += QString(" 模块7综合告警3=%1").arg(pUpsData->m_uRev[54]);
            str += QString(" 模块7综合告警4=%1").arg(pUpsData->m_uRev[55]);
            str += QString(" 模块8综合告警1=%1").arg(pUpsData->m_uRev[56]);
            str += QString(" 模块8综合告警2=%1").arg(pUpsData->m_uRev[57]);
            str += QString(" 模块8综合告警3=%1").arg(pUpsData->m_uRev[58]);
            str += QString(" 模块8综合告警4=%1").arg(pUpsData->m_uRev[59]);
            str += QString(" 模块9综合告警1=%1").arg(pUpsData->m_uRev[60]);
            str += QString(" 模块9综合告警2=%1").arg(pUpsData->m_uRev[61]);
            str += QString(" 模块9综合告警3=%1").arg(pUpsData->m_uRev[62]);
            str += QString(" 模块9综合告警4=%1").arg(pUpsData->m_uRev[63]);
            str += QString(" 模块10综合告警1=%1").arg(pUpsData->m_uRev[64]);
            str += QString(" 模块10综合告警2=%1").arg(pUpsData->m_uRev[65]);
            str += QString(" 模块10综合告警3=%1").arg(pUpsData->m_uRev[66]);
            str += QString(" 模块10综合告警4=%1").arg(pUpsData->m_uRev[67]);
            KG_LOG_INFO(QString("UPS%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x04, 20084, 2);
        if (ret)
        {
            dev.m_uCommState = 0;
            for (int i=0; i<2; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);
                pUpsData->m_uRev[0 + i] = temp16;
            }
            QString str = QString(" UPS机型=%1").arg(pUpsData->m_uRev[0]);
            str += QString(" UPS类型=%1").arg(pUpsData->m_uRev[1]);
            KG_LOG_INFO(QString("UPS%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x04, 20001, 36);
        if (ret)
        {
            dev.m_uCommState = 0;
            getFromBuff(m_pRecv + 9 + 2*(20001 - 20001), temp16);
            pUpsData->m_fRev[0] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20002 - 20001), temp16);
            pUpsData->m_fRev[1] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20003 - 20001), temp16);
            pUpsData->m_fRev[2] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20004 - 20001), temp16);
            pUpsData->m_fRev[3] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20005 - 20001), temp16);
            pUpsData->m_fRev[4] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20006 - 20001), temp16);
            pUpsData->m_fRev[5] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20007 - 20001), temp16);
            pUpsData->m_fRev[6] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20008 - 20001), temp16);
            pUpsData->m_fRev[7] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20009 - 20001), temp16);
            pUpsData->m_fRev[8] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20010 - 20001), temp16);
            pUpsData->m_fRev[9] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20011 - 20001), temp16);
            pUpsData->m_fRev[10] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20012 - 20001), temp16);
            pUpsData->m_fRev[11] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20013 - 20001), temp16);
            pUpsData->m_fRev[12] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20014 - 20001), temp16);
            pUpsData->m_fRev[13] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20015 - 20001), temp16);
            pUpsData->m_fRev[14] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20016 - 20001), temp16);
            pUpsData->m_fRev[15] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20017 - 20001), temp16);
            pUpsData->m_fRev[16] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20018 - 20001), temp16);
            pUpsData->m_fRev[17] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20019 - 20001), temp16);
            pUpsData->m_fRev[18] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20020 - 20001), temp16);
            pUpsData->m_fRev[19] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20021 - 20001), temp16);
            pUpsData->m_fRev[20] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20022 - 20001), temp16);
            pUpsData->m_fRev[21] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20023 - 20001), temp16);
            pUpsData->m_fRev[22] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20024 - 20001), temp16);
            pUpsData->m_fRev[23] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20025 - 20001), temp16);
            pUpsData->m_fRev[24] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20026 - 20001), temp16);
            pUpsData->m_fRev[25] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20027 - 20001), temp16);
            pUpsData->m_fRev[26] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20028 - 20001), temp16);
            pUpsData->m_fRev[27] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20029 - 20001), temp16);
            pUpsData->m_fRev[28] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20030 - 20001), temp16);
            pUpsData->m_fRev[29] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20031 - 20001), temp16);
            pUpsData->m_fRev[30] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20032 - 20001), temp16);
            pUpsData->m_fRev[31] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20033 - 20001), temp16);
            pUpsData->m_fRev[32] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20034 - 20001), temp16);
            pUpsData->m_fRev[33] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20035 - 20001), temp16);
            pUpsData->m_fRev[34] = (float)temp16/100;
            getFromBuff(m_pRecv + 9 + 2*(20036 - 20001), temp16);
            pUpsData->m_fRev[35] = (float)temp16/100;
            QString str = QString(" 交流旁路电压ph_A=%1").arg(pUpsData->m_fRev[0]);
            str += QString(" 交流旁路电压ph_B=%1").arg(pUpsData->m_fRev[1]);
            str += QString(" 交流旁路电压ph_C=%1").arg(pUpsData->m_fRev[2]);
            str += QString(" 交流旁路电流ph_A=%1").arg(pUpsData->m_fRev[3]);
            str += QString(" 交流旁路电流ph_B=%1").arg(pUpsData->m_fRev[4]);
            str += QString(" 交流旁路电流ph_C=%1").arg(pUpsData->m_fRev[5]);
            str += QString(" 交流旁路频率ph_A=%1").arg(pUpsData->m_fRev[6]);
            str += QString(" 交流旁路频率ph_B=%1").arg(pUpsData->m_fRev[7]);
            str += QString(" 交流旁路频率ph_C=%1").arg(pUpsData->m_fRev[8]);
            str += QString(" 交流旁路PF_A=%1").arg(pUpsData->m_fRev[9]);
            str += QString(" 交流旁路PF_B=%1").arg(pUpsData->m_fRev[10]);
            str += QString(" 交流旁路PF_C=%1").arg(pUpsData->m_fRev[11]);
            str += QString(" 交流输入电压ph_A=%1").arg(pUpsData->m_fRev[12]);
            str += QString(" 交流输入电压ph_B=%1").arg(pUpsData->m_fRev[13]);
            str += QString(" 交流输入电压ph_C=%1").arg(pUpsData->m_fRev[14]);
            str += QString(" 交流输入电流ph_A=%1").arg(pUpsData->m_fRev[15]);
            str += QString(" 交流输入电流ph_B=%1").arg(pUpsData->m_fRev[16]);
            str += QString(" 交流输入电流ph_C=%1").arg(pUpsData->m_fRev[17]);
            str += QString(" 交流输入频率ph_A=%1").arg(pUpsData->m_fRev[18]);
            str += QString(" 交流输入频率ph_B=%1").arg(pUpsData->m_fRev[19]);
            str += QString(" 交流输入频率ph_C=%1").arg(pUpsData->m_fRev[20]);
            str += QString(" 交流输入PF_A=%1").arg(pUpsData->m_fRev[21]);
            str += QString(" 交流输入PF_B=%1").arg(pUpsData->m_fRev[22]);
            str += QString(" 交流输入PF_C=%1").arg(pUpsData->m_fRev[23]);
            str += QString(" 交流输出电压ph_A=%1").arg(pUpsData->m_fRev[24]);
            str += QString(" 交流输出电压ph_B=%1").arg(pUpsData->m_fRev[25]);
            str += QString(" 交流输出电压ph_C=%1").arg(pUpsData->m_fRev[26]);
            str += QString(" 交流输出电流ph_A=%1").arg(pUpsData->m_fRev[27]);
            str += QString(" 交流输出电流ph_B=%1").arg(pUpsData->m_fRev[28]);
            str += QString(" 交流输出电流ph_C=%1").arg(pUpsData->m_fRev[29]);
            str += QString(" 交流输出频率ph_A=%1").arg(pUpsData->m_fRev[30]);
            str += QString(" 交流输出频率ph_B=%1").arg(pUpsData->m_fRev[31]);
            str += QString(" 交流输出频率ph_C=%1").arg(pUpsData->m_fRev[32]);
            str += QString(" 交流输出PF_A=%1").arg(pUpsData->m_fRev[33]);
            str += QString(" 交流输出PF_B=%1").arg(pUpsData->m_fRev[34]);
            str += QString(" 交流输出PF_C=%1").arg(pUpsData->m_fRev[35]);
            KG_LOG_INFO(QString("UPS%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x04, 20037, 37);
        if (ret)
        {
            dev.m_uCommState = 0;
            for (int i=0; i<15; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);
                pUpsData->m_fRev[36 + i] = (float)temp16/10;
            }
            getFromBuff(m_pRecv + 9 + 2*(20052 - 20037), temp16);
            pUpsData->m_fRev[51] = (float)((signed short)temp16)/10;
            getFromBuff(m_pRecv + 9 + 2*(20053 - 20037), temp16);
            pUpsData->m_fRev[52] = (float)((signed short)temp16)/10;
            for (int i=0; i<14; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(20054 - 20037 + i), temp16);
                pUpsData->m_fRev[53 + i] = (float)temp16/10;
            }
            getFromBuff(m_pRecv + 9 + 2*(20068 - 20037), temp16);
            pUpsData->m_fRev[67] = temp16;
            getFromBuff(m_pRecv + 9 + 2*(20069 - 20037), temp16);
            pUpsData->m_fRev[68] = temp16;
            getFromBuff(m_pRecv + 9 + 2*(20070 - 20037), temp16);
            pUpsData->m_fRev[69] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20071 - 20037), temp16);
            pUpsData->m_fRev[70] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20072 - 20037), temp16);
            pUpsData->m_fRev[71] = temp16;
            getFromBuff(m_pRecv + 9 + 2*(20073 - 20037), temp16);
            pUpsData->m_fRev[72] = temp16;
            QString str = QString(" 输出视在功率ph_A=%1").arg(pUpsData->m_fRev[36]);
            str += QString(" 输出视在功率ph_B=%1").arg(pUpsData->m_fRev[37]);
            str += QString(" 输出视在功率ph_C=%1").arg(pUpsData->m_fRev[38]);
            str += QString(" 输出有功功率ph_A=%1").arg(pUpsData->m_fRev[39]);
            str += QString(" 输出有功功率ph_B=%1").arg(pUpsData->m_fRev[40]);
            str += QString(" 输出有功功率ph_C=%1").arg(pUpsData->m_fRev[41]);
            str += QString(" 输出无功功率ph_A=%1").arg(pUpsData->m_fRev[42]);
            str += QString(" 输出无功功率ph_B=%1").arg(pUpsData->m_fRev[43]);
            str += QString(" 输出无功功率ph_C=%1").arg(pUpsData->m_fRev[44]);
            str += QString(" 负载百分数ph_A=%1").arg(pUpsData->m_fRev[45]);
            str += QString(" 负载百分数ph_B=%1").arg(pUpsData->m_fRev[46]);
            str += QString(" 负载百分数ph_C=%1").arg(pUpsData->m_fRev[47]);
            str += QString(" 环境温度=%1").arg(pUpsData->m_fRev[48]);
            str += QString(" 正电池组电压=%1").arg(pUpsData->m_fRev[49]);
            str += QString(" 负电池组电压=%1").arg(pUpsData->m_fRev[50]);
            str += QString(" 正电池组电流=%1").arg(pUpsData->m_fRev[51]);
            str += QString(" 负电池组电流=%1").arg(pUpsData->m_fRev[52]);
            str += QString(" 电池温度=%1").arg(pUpsData->m_fRev[53]);
            str += QString(" 电池剩余时间=%1").arg(pUpsData->m_fRev[54]);
            str += QString(" 电池容量=%1").arg(pUpsData->m_fRev[55]);
            str += QString(" 交流旁路线电压ph_A=%1").arg(pUpsData->m_fRev[56]);
            str += QString(" 交流旁路线电压ph_B=%1").arg(pUpsData->m_fRev[57]);
            str += QString(" 交流旁路线电压ph_C=%1").arg(pUpsData->m_fRev[58]);
            str += QString(" 交流主路线电压ph_A=%1").arg(pUpsData->m_fRev[59]);
            str += QString(" 交流主路线电压ph_B=%1").arg(pUpsData->m_fRev[60]);
            str += QString(" 交流主路线电压ph_C=%1").arg(pUpsData->m_fRev[61]);
            str += QString(" 交流输出线电压ph_A=%1").arg(pUpsData->m_fRev[62]);
            str += QString(" 交流输出线电压ph_B=%1").arg(pUpsData->m_fRev[63]);
            str += QString(" 交流输出线电压ph_C=%1").arg(pUpsData->m_fRev[64]);
            str += QString(" 旁路SCR温度1=%1").arg(pUpsData->m_fRev[65]);
            str += QString(" 旁路SCR温度2=%1").arg(pUpsData->m_fRev[66]);
            str += QString(" 旁路风扇运行时间=%1").arg(pUpsData->m_fRev[67]);
            str += QString(" 防尘网已使用天数=%1").arg(pUpsData->m_fRev[68]);
            str += QString(" 电池总运行时间%1").arg(pUpsData->m_fRev[69]);
            str += QString(" 电池总放电时间=%1").arg(pUpsData->m_fRev[70]);
            str += QString(" 电池连续未放电时间=%1").arg(pUpsData->m_fRev[71]);
            str += QString(" 电池放电次数=%1").arg(pUpsData->m_fRev[72]);
            KG_LOG_INFO(QString("UPS%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
        ret = readRegData(dev.m_uAddress, 0x04, 20096, 6);
        if (ret)
        {
            dev.m_uCommState = 0;
            getFromBuff(m_pRecv + 9 + 2*(20096 - 20096), temp16);
            pUpsData->m_fRev[73] = temp16;
            getFromBuff(m_pRecv + 9 + 2*(20097 - 20096), temp16);
            pUpsData->m_fRev[74] = temp16;
            getFromBuff(m_pRecv + 9 + 2*(20098 - 20096), temp16);
            pUpsData->m_fRev[75] = temp16;
            getFromBuff(m_pRecv + 9 + 2*(20099 - 20096), temp16);
            pUpsData->m_fRev[76] = temp16;
            getFromBuff(m_pRecv + 9 + 2*(20100 - 20096), temp16);
            pUpsData->m_fRev[77] = (float)temp16/10;
            getFromBuff(m_pRecv + 9 + 2*(20101 - 20096), temp16);
            pUpsData->m_fRev[78] = (float)temp16/10;
            QString str = QString(" 额定输入电压=%1").arg(pUpsData->m_fRev[73]);
            str += QString(" 额定输入频率=%1").arg(pUpsData->m_fRev[74]);
            str += QString(" 额定输出电压=%1").arg(pUpsData->m_fRev[75]);
            str += QString(" 额定输出频率=%1").arg(pUpsData->m_fRev[76]);
            str += QString(" 机柜额定使用功率=%1").arg(pUpsData->m_fRev[77]);
            str += QString(" 模块额定使用功率=%1").arg(pUpsData->m_fRev[78]);
            KG_LOG_INFO(QString("UPS%1%2").arg(index+1).arg(str));
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
    }while(0);
    if (!pUpsData->m_uCommState)
    {
        // 无
    }
}

void EthThread::readBmsData(int index)
{
    switch(dev.m_uFunc)
    {
        case 2090: funcBms2090(dev, index); break; //【奥威】【PCS与BMS通信点表】
    }
    m_bOpen = !dev.m_uCommState;
    if (dev.m_uCommLastState != dev.m_uCommState)
    {
        if ((dev.m_Time.secsTo(QDateTime::currentDateTime()) >= dev.m_uCommTimeout) || (dev.m_uCommState == 0))
        {
            dev.m_Time = QDateTime::currentDateTime();
            dev.m_uCommLastState = dev.m_uCommState;
            // ? = dev.m_uCommState;
            QString str = QString("连接逆变器%1 BMS通信%2").arg(index+1).arg(dev.m_uCommState?"失败":"成功");
            KG_LOG_INFO(str);
            saveLogToBaDb(str);  
        }    
    }
    else
    {
        dev.m_Time = QDateTime::currentDateTime();
    } 
}

void EthThread::funcBms2090(devData &dev, int index) //【奥威】【PCS与BMS通信点表】
{
    do
    {
        quint16 a[10] = {0};
        // 工作状态信息 0x1111禁充 0x2222禁放 0x5555待机 0xAAAA跳机 0xBBBB正常 0xCCCC预警
        a[0] = m_pBattStack->usStackState;
        // 最大允许充电电流 0.1A
        a[1] = (quint16)(m_pBattStack->fStackAllowMaxChargeCurrent*10);
        // 最大允许放电电流 0.1A
        a[2] = (quint16)(m_pBattStack->fStackAllowMaxDischargeCurrent*10);
        // SOC 0.1%
        a[3] = (quint16)(m_pBattStack->fStackSoc*10);
        // 可充电量 0.1kWh
        a[4] = (quint16)(m_pBattStack->fStackChargeEQ*10);
        // 可放电量 0.1kWh
        a[5] = (quint16)(m_pBattStack->fStackDischargeEQ*10);
        // 总电压 0.1V
        a[6] = (quint16)(m_pBattStack->fStackVol*10);
        // 总电流 0.1A
        a[7] = (signed short)(m_pBattStack->fStackCur*10);
        // 最大允许充电功率 0.1kW
        a[8] = (quint16)(m_pBattStack->fStackAllowMaxChargePower*10);
        // 最大允许放电功率 0.1kW
        a[9] = (quint16)(m_pBattStack->fStackAllowMaxDischargePower*10);
        ret = setMultipleRegData(dev.m_uAddress, 0x10, 1, 10, a);
        if (ret)
        {
            dev.m_uCommState = 0;
            QString str = QString(" 工作状态=%1").arg(m_pBattStack->usStackState,0,16).toUpper();
            str += QString(" 最大允许充电电流=%1").arg(m_pBattStack->fStackAllowMaxChargeCurrent);
            str += QString(" 最大允许放电电流=%1").arg(m_pBattStack->fStackAllowMaxDischargeCurrent);
            str += QString(" 电池堆SOC=%1").arg(m_pBattStack->fStackSoc);
            str += QString(" 电池堆可充电量=%1").arg(m_pBattStack->fStackChargeEQ);
            str += QString(" 电池堆可放电量=%1").arg(m_pBattStack->fStackDischargeEQ);
            str += QString(" 电池堆总电压=%1").arg(m_pBattStack->fStackVol);
            str += QString(" 电池堆总电流=%1").arg(m_pBattStack->fStackCur);
            str += QString(" 最大允许充电功率=%1").arg(m_pBattStack->fStackAllowMaxChargePower);
            str += QString(" 最大允许放电功率=%1").arg(m_pBattStack->fStackAllowMaxDischargePower);
            KG_LOG_INFO(QString("逆变器%1下发%2").arg(index+1).arg(str));                
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }   
    }while(0);
}

void EthThread::readBcmuData(int index)
{
    CBattBank *pBattBank = m_pRTDB->getBank(index); 
    switch(dev.m_uFunc)
    {
        default: funcBcmu(dev, pBattBank, index); break; //【BCMU与后台tcp modbus通讯点表V2.8】
    }    
    m_bOpen = !dev.m_uCommState;
    if (dev.m_uCommLastState != dev.m_uCommState)
    {
        if ((dev.m_Time.secsTo(QDateTime::currentDateTime()) >= dev.m_uCommTimeout) || (dev.m_uCommState == 0))
        {
            dev.m_Time = QDateTime::currentDateTime();
            dev.m_uCommLastState = dev.m_uCommState;
            pBattBank->aBCMUConnFault = dev.m_uCommState;
            QString str = QString("连接BC%1 通信%2").arg(index+1).arg(dev.m_uCommState?"失败":"成功");
            KG_LOG_INFO(str);
            // saveLogToBaDb(str);  
        }    
    }
    else
    {
        dev.m_Time = QDateTime::currentDateTime();
    }    
}

void EthThread::funcBcmu(devData &dev, CBattBank *pBattBank, int index) //【BCMU与后台tcp modbus通讯点表V2.8】
{
    Q_UNUSED(index);
    do
    {
    //@ 单体电压
        ret = readRegData(dev.m_uAddress, 0x04, 0, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 0~99 单体(1~100)电压 0.001
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglVol[0 + i] = float(temp16)/1000;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 100, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 100~199 单体(101~200)电压 0.001
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglVol[100 + i] = float(temp16)/1000;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 200, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 200~299 单体(201~300)电压 0.001
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglVol[200 + i] = float(temp16)/1000;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 300, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 300~399 单体(301~400)电压 0.001
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglVol[300 + i] = float(temp16)/1000;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 3000, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 3000~3099 单体(401~500)电压 0.001
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglVol[400 + i] = float(temp16)/1000;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
    //@ 单体温度
        ret = readRegData(dev.m_uAddress, 0x04, 400, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 400~499 单体(1~100)温度 0.1 偏移80
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglTemp[0 + i] = ((float)temp16-800)/10;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 500, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 500~599 单体(101~200)温度 0.1 偏移80
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglTemp[100 + i] = ((float)temp16-800)/10;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 600, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 600~699 单体(201~300)温度 0.1 偏移80
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglTemp[200 + i] = ((float)temp16-800)/10;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 700, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 700~799 单体(301~400)温度 0.1 偏移80
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglTemp[300 + i] = ((float)temp16-800)/10;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }  
        ret = readRegData(dev.m_uAddress, 0x04, 3400, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 3400~3499 单体(401~500)温度 0.1 偏移80
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglTemp[400 + i] = ((float)temp16-800)/10;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }              
    //@ 单体SOC
        ret = readRegData(dev.m_uAddress, 0x04, 800, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 800~899 单体(1~100)SOC %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoc[0 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 900, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 900~999 单体(101~200)SOC %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoc[100 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 1000, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1000~1099 单体(201~300)SOC %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoc[200 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 1100, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1100~1199 单体(301~400)SOC %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoc[300 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }  
        ret = readRegData(dev.m_uAddress, 0x04, 3800, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 3800~3899 单体(401~500)SOC %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoc[400 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }            
    //@ 单体SOH
        ret = readRegData(dev.m_uAddress, 0x04, 1200, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1200~1299 单体(1~100)SOH %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoh[0 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 1300, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1300~1399 单体(101~200)SOH %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoh[100 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 1400, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1400~1499 单体(201~300)SOH %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoh[200 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 1500, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1500~1599 单体(301~400)SOH %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoh[300 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }  
        ret = readRegData(dev.m_uAddress, 0x04, 4200, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 4200~4299 单体(401~500)SOH %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglSoh[400 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }            
    //@ 单体内阻
        ret = readRegData(dev.m_uAddress, 0x04, 1600, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1600~1699 单体(1~100)内阻 %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglRes[0 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 1700, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1700~1799 单体(101~200)内阻 %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglRes[100 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 1800, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1800~1899 单体(201~300)内阻 %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglRes[200 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
        ret = readRegData(dev.m_uAddress, 0x04, 1900, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 1900~1999 单体(301~400)内阻 %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglRes[300 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }  
        ret = readRegData(dev.m_uAddress, 0x04, 4600, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 4600~4699 单体(401~500)内阻 %1
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fSglRes[400 + i] = temp16;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }  
    //@ 充电均衡状态
        ret = readRegData(dev.m_uAddress, 0x04, 2650, 25);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 2650~2674 充电均衡状态(1~400)
            for (int i=0; i<25; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                for (int j=0; j<16; j++)
                {
                    pBattBank->aChgEquState[i*16 + j] = (temp16>>j)&0x01;
                }
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }  
        ret = readRegData(dev.m_uAddress, 0x04, 5650, 13);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 5650~5661 充电均衡状态(401~592)
            for (int i=0; i<12; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                for (int j=0; j<16; j++)
                {
                    pBattBank->aChgEquState[400 + i*16 + j] = (temp16>>j)&0x01;
                }
            }
            // 5662 充电均衡状态(593~600)
            getFromBuff(m_pRecv + 9 + 2*(5662 - 5650), temp16);    
            for (int j=0; j<8; j++)
            {
                pBattBank->aChgEquState[592 + j] = (temp16>>j)&0x01;
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }          
    //@ 放电均衡状态
        ret = readRegData(dev.m_uAddress, 0x04, 2675, 25);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 2675~2699 放电均衡状态(1~400)
            for (int i=0; i<25; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                for (int j=0; j<16; j++)
                {
                    pBattBank->aDchgEquState[i*16 + j] = (temp16>>j)&0x01;
                }
            }
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }       
        ret = readRegData(dev.m_uAddress, 0x04, 5675, 13);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 5675~5686 放电均衡状态(401~592)
            for (int i=0; i<12; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                for (int j=0; j<16; j++)
                {
                    pBattBank->aDchgEquState[400 + i*16 + j] = (temp16>>j)&0x01;
                }
            }
            // 5687 放电均衡状态(593~600)
            getFromBuff(m_pRecv + 9 + 2*(5687 - 5675), temp16);    
            for (int j=0; j<8; j++)
            {
                pBattBank->aDchgEquState[592 + j] = (temp16>>j)&0x01;
            }            
            msleep(dev.m_uDelayMs);                
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }         
    //@ PCAK电压
        ret = readRegData(dev.m_uAddress, 0x04, 5000, 40);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 5000~5039 PCAK(1~40)总压 0.1V
            for (int i=0; i<40; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fPackVol[0 + i] = (float)temp16/10;
            }
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }     
    //@ 动力线温度
        ret = readRegData(dev.m_uAddress, 0x04, 6000, 100);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 6000~6099 动力线温度(1~100) 0.1℃ 偏移80
            for (int i=0; i<100; i++)
            {
                getFromBuff(m_pRecv + 9 + 2*(i), temp16);    
                pBattBank->fPwLnTemp[0 + i] = ((float)temp16-800)/10;
            }
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }      
    //@ 组电压
        ret = readRegData(dev.m_uAddress, 0x04, 8000, 40);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 8003 DI
            getFromBuff(m_pRecv + 9 + 2*(8003 - 8000), temp16);    
            pBattBank->usDI1 = (temp16&0x01)?1:0;
            pBattBank->usDI2 = (temp16&0x02)?1:0;
            pBattBank->usDI3 = (temp16&0x04)?1:0;
            pBattBank->usDI4 = (temp16&0x08)?1:0;
            pBattBank->usDI5 = (temp16&0x10)?1:0;
            pBattBank->usDI6 = (temp16&0x20)?1:0;
            pBattBank->aBankTemp[0] = (temp16&0x40)?1:0; // DI7
            pBattBank->aBankTemp[1] = (temp16&0x80)?1:0; // DI8
            pBattBank->aBankTemp[2] = (temp16&0x100)?1:0; // DI9
            pBattBank->aBankTemp[3] = (temp16&0x200)?1:0; // DI10
            pBattBank->aBankTemp[4] = (temp16&0x400)?1:0; // DI11
            pBattBank->aBankTemp[5] = (temp16&0x800)?1:0; // DI12                
            // 8004 DO
            getFromBuff(m_pRecv + 9 + 2*(8004 - 8000), temp16);    
            pBattBank->usWetState = (temp16&0x01)?1:0;
            pBattBank->usDry1State = (temp16&0x02)?1:0;
            pBattBank->usDry2State = (temp16&0x04)?1:0;
            pBattBank->usDry3State = (temp16&0x08)?1:0;
            pBattBank->usDry4State = (temp16&0x10)?1:0;
            pBattBank->usDry5State = (temp16&0x20)?1:0;
            pBattBank->usDry6State = (temp16&0x40)?1:0;
            pBattBank->usDry7State = (temp16&0x80)?1:0;
            pBattBank->usBankTemp[0] = (temp16&0x100)?1:0;
            pBattBank->usBankTemp[1] = (temp16&0x200)?1:0;
            pBattBank->usBankTemp[2] = (temp16&0x400)?1:0;
            pBattBank->usBankTemp[3] = (temp16&0x800)?1:0;            
            // 8005 组电压 0.1V
            getFromBuff(m_pRecv + 9 + 2*(8005 - 8000), temp16);    
            pBattBank->fBankVol = (float)temp16/10;
            // 8006 组电流 0.1A 偏移3000
            getFromBuff(m_pRecv + 9 + 2*(8006 - 8000), temp16);    
            pBattBank->fBankCur = ((float)temp16-30000)/10;
            // 8007 心跳 秒
            getFromBuff(m_pRecv + 9 + 2*(8007 - 8000), temp32);   
            pBattBank->uiBCHeartbeat = temp32;
            // 8009~8012 组端温度 0.1 80
            getFromBuff(m_pRecv + 9 + 2*(8009 - 8000), temp16);    
            pBattBank->fEnvTemp = pBattBank->fHvPwlTemp[0] = ((float)temp16-800)/10;
            getFromBuff(m_pRecv + 9 + 2*(8010 - 8000), temp16);    
            pBattBank->fEnvTemp2 = pBattBank->fHvPwlTemp[1] = ((float)temp16-800)/10;
            getFromBuff(m_pRecv + 9 + 2*(8011 - 8000), temp16);    
            pBattBank->fEnvTemp3 = pBattBank->fHvPwlTemp[2] = ((float)temp16-800)/10;
            getFromBuff(m_pRecv + 9 + 2*(8012 - 8000), temp16);    
            pBattBank->fEnvTemp4 = pBattBank->fHvPwlTemp[3] = ((float)temp16-800)/10;
            // 8013 正极绝缘 KΩ
            getFromBuff(m_pRecv + 9 + 2*(8013 - 8000), temp32);    
            pBattBank->fPosRes = (float)temp32/1000;
            // 8015 负极绝缘 KΩ
            getFromBuff(m_pRecv + 9 + 2*(8015 - 8000), temp32);    
            pBattBank->fNegRes = (float)temp32/1000;
            // 8018 SOC %1
            getFromBuff(m_pRecv + 9 + 2*(8018 - 8000), temp16);    
            if ((m_pBattStack->usBankSocStrategy == 2) || (m_pBattStack->usBankSocStrategy == 3))
            {// 2或3 BC计算组SOC
                pBattBank->fBankSoc = temp16;
            }
            // 8019 SOH %1
            getFromBuff(m_pRecv + 9 + 2*(8019 - 8000), temp16);    
            pBattBank->fBankSoh = temp16;
            // 8020 BC故障类型
            getFromBuff(m_pRecv + 9 + 2*(8020 - 8000), temp16);
            pBattBank->usActRsn = temp16;
            // 8021 BC故障次数
            getFromBuff(m_pRecv + 9 + 2*(8021 - 8000), temp16);
            pBattBank->usActCount = temp16;
            // 8022 BMU掉线位置
            getFromBuff(m_pRecv + 9 + 2*(8022 - 8000), temp16);
            pBattBank->usBMUConnFaultNo = temp16;
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }    
    //@ BC和BMU之间的通讯故障
        ret = readRegData(dev.m_uAddress, 0x04, 9039, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 9039 bit14 BC和BMU之间的通讯故障
            getFromBuff(m_pRecv + 9 + 2*(9039 - 9039), temp16);
            pBattBank->aBMUConnFault = ((!(temp16&(1<<14))) || (pBattBank->usBMUConnFaultNo == 0))?0:1;
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }  
    //@ 绝缘采集完成标志位
        ret = readRegData(dev.m_uAddress, 0x04, 9043, 1);
        if (ret)
        {
            dev.m_uCommState = 0;
            // 9043 bit13 绝缘采集完成标志位 1成功
            getFromBuff(m_pRecv + 9 + 2*(9043 - 9043), temp16);
            if (pBattBank->usInitFulfillNum < 2)
            {
                pBattBank->usInitFulfillNum += (temp16&(1<<13))?1:0;
            }
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }            
    //@ 写DO
        quint16 a[8] = {0};
        // 1000 湿接点1
        a[0] = getWetCtrl(pBattBank);
        a[1] = getDoCtrl(pBattBank->iDry1CtrlNum, pBattBank->usDry1Ctrl, pBattBank->usDry1State);
        a[2] = getDoCtrl(pBattBank->iDry2CtrlNum, pBattBank->usDry2Ctrl, pBattBank->usDry2State);
        a[3] = getDoCtrl(pBattBank->iDry3CtrlNum, pBattBank->usDry3Ctrl, pBattBank->usDry3State);
        a[4] = getDoCtrl(pBattBank->iDry4CtrlNum, pBattBank->usDry4Ctrl, pBattBank->usDry4State);
        a[5] = getDoCtrl(pBattBank->iDry5CtrlNum, pBattBank->usDry5Ctrl, pBattBank->usDry5State);
        a[6] = getDoCtrl(pBattBank->iDry6CtrlNum, pBattBank->usDry6Ctrl, pBattBank->usDry6State);
        a[7] = getDoCtrl(pBattBank->iDry7CtrlNum, pBattBank->usDry7Ctrl, pBattBank->usDry7State);
        ret = setMultipleRegData(dev.m_uAddress, 0x10, 1000, 8, a);
        if (ret)
        {
            dev.m_uCommState = 0;
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }  
    //@ 03 1031~1034 写DO
        a[0] = getDoCtrl(pBattBank->usBankTemp[8], pBattBank->usBankTemp[4], pBattBank->usBankTemp[0]);
        a[1] = getDoCtrl(pBattBank->usBankTemp[9], pBattBank->usBankTemp[5], pBattBank->usBankTemp[1]);
        a[2] = getDoCtrl(pBattBank->usBankTemp[10], pBattBank->usBankTemp[6], pBattBank->usBankTemp[2]);
        a[3] = getDoCtrl(pBattBank->usBankTemp[11], pBattBank->usBankTemp[7], pBattBank->usBankTemp[3]);
        ret = setMultipleRegData(dev.m_uAddress, 0x10, 1031, 4, a);
        if (ret)
        {
            dev.m_uCommState = 0;
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }          
    //@ SOC初始化 
        if (pBattBank->usResetSOC != 0xFFFF)
        {
            ret = setSingleRegData(dev.m_uAddress, 0x06, 1013, pBattBank->usResetSOC);
            if (ret)
            {
                dev.m_uCommState = 0;
                pBattBank->usResetSOC = 0xFFFF;
                msleep(dev.m_uDelayMs);
            }
            else
            {
                dev.m_uCommState = 1;
                break;
            }
        }
    //@ 均衡使能
        if (m_pBattStack->usEquiEnabled != 0xFFFF)
        {
            ret = setSingleRegData(dev.m_uAddress, 0x06, 1195, m_pBattStack->usEquiEnabled);
            if (ret)
            {
                dev.m_uCommState = 0;
                msleep(dev.m_uDelayMs);
            }
            else
            {
                dev.m_uCommState = 1;
                break;
            }
        }  
    //@ BA下发工作状态 2正常 3 预警 4待机 5跳机 6电压禁充 7电压禁放 8其他禁充 9其他禁放
        if (m_pBattStack->usDCSideConfType == 0) temp16 = m_pBattStack->usStackState; // 并连汇入
        else temp16 = pBattBank->usBankState; 
        switch(temp16)
        {
            case 0x1111: 
            {
                if (pBattBank->usForbidReason & 0x0100) temp16 = 6; // 电压禁充
                else temp16 = 8;                                      // 其他禁充
            } 
            break;
            case 0x2222: 
            {
                if (pBattBank->usForbidReason & 0x0001) temp16 = 7; // 电压禁放
                else temp16 = 9;                                      // 其他禁放
            }
            break;
            case 0xBBBB: temp16 = 2; break; // 正常
            case 0xCCCC: temp16 = 3; break; // 预警
            case 0x5555: temp16 = 4; break; // 待机
            default: temp16 = 5; break;     // 故障
        }
        ret = setSingleRegData(dev.m_uAddress, 0x06, 1010, temp16);
        if (ret)
        {
            dev.m_uCommState = 0;
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }
    //@ BA下发最大允许充放电电流、功率
        quint16 b[4] = {0,0,0,0};    
        b[0] = (quint16)(pBattBank->fBankAllowMaxChargeCurrent*10);
        b[1] = (quint16)(pBattBank->fBankAllowMaxDischargeCurrent*10);
        b[2] = (quint16)(pBattBank->fBankAllowMaxChargePower*10);
        b[3] = (quint16)(pBattBank->fBankAllowMaxDischargePower*10);
        ret = setMultipleRegData(dev.m_uAddress, 0x10, 1026, 4, b);
        if (ret)
        {
            dev.m_uCommState = 0;
            msleep(dev.m_uDelayMs);
        }
        else
        {
            dev.m_uCommState = 1;
            break;
        }  
    }while(0);
}

quint16 EthThread::getWetCtrl(CBattBank *pBattBank)
{
    quint16 ret = 0xFFFF;
    if (pBattBank->iWetCtrlNum != 0)
    {
        ret = pBattBank->usWetCtrl?1:0;
        if (((m_pBattStack->usCtrlLoopType == 3) && (((pBattBank->usWetState > 0) && (pBattBank->usWetCtrl) > 0) || ((pBattBank->usWetState == 0) && (pBattBank->usWetCtrl == 0))))
        || ((m_pBattStack->usCtrlLoopType != 3) && (((pBattBank->usDI1 > 0) && (pBattBank->usWetCtrl) > 0) || ((pBattBank->usDI1 == 0) && (pBattBank->usWetCtrl == 0)))))
        {
            if (pBattBank->iWetCtrlNum > 0) pBattBank->iWetCtrlNum--;
        }
    }
    return ret;
}

quint16 EthThread::getDoCtrl(int &iDryCtrlNum, quint16 usDryCtrl, quint16 usDryState)
{ // 传引用(&a):可改变实参
    quint16 ret = 0xFFFF;
    if (iDryCtrlNum != 0)
    {
        ret = usDryCtrl?1:0;
        if (usDryState == usDryCtrl)
        {
            if (iDryCtrlNum > 0) iDryCtrlNum--;
        }
    }
    return ret;
}

quint16 EthThread::getDoCtrl(quint16 &uDryCtrlNum, quint16 usDryCtrl, quint16 usDryState)
{ // 传引用(&a):可改变实参
    quint16 ret = 0xFFFF;
    if (uDryCtrlNum != 0)
    {
        ret = usDryCtrl?1:0;
        if (usDryState == usDryCtrl)
        {
            if (uDryCtrlNum > 0) uDryCtrlNum--;
        }
    }
    return ret;
}

int EthThread::tcpInit(QString m_serverIP, quint16 m_serverPort)
{
    if (m_tcpSocket)
    {
        m_tcpSocket->close();    
        delete m_tcpSocket;
        m_tcpSocket = NULL;
    }    
    m_tcpSocket = new QTcpSocket();
    m_tcpSocket->connectToHost(m_serverIP, m_serverPort);
    int ret = m_tcpSocket->waitForConnected(1000);
    KG_LOG_INFO(QString("TCP连接服务器%1 %2 %3").arg(m_serverIP).arg(m_serverPort).arg(ret?"成功":"失败"));    
    return (ret?1:0);
}

int EthThread::readRegData(quint8 devAddr,quint8 funCode,quint16 startAddr,quint16 regCnt)
{
    // 发送数据
    memset(m_pSend,0,sizeof(m_pSend));
    m_uMbap++; // 事务标识符号 每次+1   
    m_pSend[0] = (quint8)(m_uMbap>>8);
    m_pSend[1] = (quint8)(m_uMbap);  
    m_pSend[2] = 0; // 协议标识 代表TCP协议
    m_pSend[3] = 0;  
    m_pSend[4] = 0;
    m_pSend[5] = 6; // 后续字节长度
    m_pSend[6] = devAddr;
    m_pSend[7] = funCode;
    m_pSend[8] = (quint8)(startAddr>>8);
    m_pSend[9] = (quint8)(startAddr);
    m_pSend[10] = (quint8)(regCnt>>8);
    m_pSend[11] = (quint8)(regCnt);
    int ret = m_tcpSocket->write(m_pSend, 12);
    if (ret == -1)
    {
        KG_LOG_INFO("发送失败");
        return 0;
    }
    QString str = "发: ";
    for (int i=0; i<12; i++)
    {
        str += QString("%1 ").arg((uchar)*(m_pSend+i),2,16,(QChar)'0');
    }
    str = str.toUpper();
    KG_LOG_INFO(QString("%1").arg(str));
    m_tcpSocket->waitForBytesWritten();
    m_tcpSocket->flush();
    int totalLength = 0;
    if (funCode == 0x03 || funCode == 0x04) totalLength = 6 + regCnt*2 + 3;
    else if (funCode == 0x01 || funCode == 0x02) totalLength = 6 + (regCnt+7)/8 + 3;  
    // 接收数据
    ret = m_tcpSocket->waitForReadyRead(1500);
    if (!ret)
    {
        KG_LOG_INFO("接收失败");
        return 0;
    }
    memset(m_pRecv,0,sizeof(m_pRecv));
    ret = m_tcpSocket->read(m_pRecv,sizeof(m_pRecv));
    if (ret == totalLength)
    {
        str = "收: ";
        for (int i=0; i<totalLength; i++)
        {
            str += QString("%1 ").arg((uchar)*(m_pRecv+i),2,16,(QChar)'0');
        }
        str = str.toUpper();
        KG_LOG_INFO(QString("%1").arg(str));      
        if((m_pRecv[0] == m_pSend[0]) 
        && (m_pRecv[1] == m_pSend[1]) // 事务标识
        && (m_pRecv[2] == m_pSend[2])
        && (m_pRecv[3] == m_pSend[3]) // 协议标识
        && ((m_pRecv[4]*256+m_pRecv[5]) == (totalLength - 6))
        && (m_pRecv[6] == m_pSend[6]) // 地址
        && (m_pRecv[7] == m_pSend[7]) // 命令
        && (m_pRecv[8] == (totalLength - 9)))
        {
            return 1;
        }
    }
    else
    {
        KG_LOG_INFO(QString("接收应答超时=%1<%2").arg(ret).arg(totalLength));
    }
    return 0;
}

int EthThread::setSingleRegData(quint8 devAddr,quint8 funCode,quint16 startAddr,quint16 regVal)
{
    // 发送数据
    memset(m_pSend,0,sizeof(m_pSend));
    m_uMbap++; // 事务标识符号 每次+1   
    m_pSend[0] = (quint8)(m_uMbap>>8);
    m_pSend[1] = (quint8)(m_uMbap);  
    m_pSend[2] = 0; // 协议标识 代表TCP协议
    m_pSend[3] = 0;  
    m_pSend[4] = 0;
    m_pSend[5] = 6; // 后续字节长度
    m_pSend[6] = devAddr;
    m_pSend[7] = funCode;
    m_pSend[8] = (quint8)(startAddr>>8);
    m_pSend[9] = (quint8)(startAddr);
    m_pSend[10] = (quint8)(regVal>>8);
    m_pSend[11] = (quint8)(regVal);
    int ret = m_tcpSocket->write(m_pSend, 12);
    if (ret == -1)
    {
        KG_LOG_INFO("发送失败");
        return 0;
    }
    QString str = "发: ";
    for (int i=0; i<12; i++)
    {
        str += QString("%1 ").arg((uchar)*(m_pSend+i),2,16,(QChar)'0');
    }
    str = str.toUpper();
    KG_LOG_INFO(QString("%1").arg(str));
    m_tcpSocket->waitForBytesWritten();
    m_tcpSocket->flush();
    int totalLength = 12;
    // 接收数据
    ret = m_tcpSocket->waitForReadyRead(1500);
    if (!ret)
    {
        KG_LOG_INFO("接收失败");
        return 0;
    }
    memset(m_pRecv,0,sizeof(m_pRecv));
    ret = m_tcpSocket->read(m_pRecv,sizeof(m_pRecv));
    if (ret == totalLength)
    {
        str = "收: ";
        for (int i=0; i<totalLength; i++)
        {
            str += QString("%1 ").arg((uchar)*(m_pRecv+i),2,16,(QChar)'0');
        }
        str = str.toUpper();
        KG_LOG_INFO(QString("%1").arg(str));      
        if((m_pRecv[0] == m_pSend[0]) 
        && (m_pRecv[1] == m_pSend[1]) // 事务标识
        && (m_pRecv[2] == m_pSend[2])
        && (m_pRecv[3] == m_pSend[3]) // 协议标识
        && ((m_pRecv[4]*256+m_pRecv[5]) == 6)
        && (m_pRecv[6] == m_pSend[6]) // 地址
        && (m_pRecv[7] == m_pSend[7]) // 命令
        && (m_pRecv[8] == m_pSend[8])
        && (m_pRecv[9] == m_pSend[9])
        && (m_pRecv[10] == m_pSend[10])
        && (m_pRecv[11] == m_pSend[11]))        
        {
            return 1;
        }
    }
    else
    {
        KG_LOG_INFO(QString("接收应答超时=%1<%2").arg(ret).arg(totalLength));
    }
    return 0;
}

int EthThread::setMultipleRegData(quint8 devAddr,quint8 funCode,quint16 startAddr,quint16 regCnt,quint16 *dataBuf)
{
    // 发送数据
    memset(m_pSend,0,sizeof(m_pSend));
    m_uMbap++; // 事务标识符号 每次+1   
    m_pSend[0] = (quint8)(m_uMbap>>8);
    m_pSend[1] = (quint8)(m_uMbap);  
    m_pSend[2] = 0; // 协议标识 代表TCP协议
    m_pSend[3] = 0;  
    m_pSend[4] = 0;
    m_pSend[5] = 7+2*regCnt; // 后续字节长度
    m_pSend[6] = devAddr;
    m_pSend[7] = funCode;
    m_pSend[8] = (quint8)(startAddr>>8);
    m_pSend[9] = (quint8)(startAddr);
    m_pSend[10] = (quint8)(regCnt>>8);
    m_pSend[11] = (quint8)(regCnt);
    m_pSend[12] = 2*regCnt;
    for (int i=0; i<regCnt; i++)
    {
        m_pSend[13+i*2] = (quint8)((*(dataBuf+i))>>8);
        m_pSend[14+i*2] = (quint8)(*(dataBuf+i));
    }
    int ret = m_tcpSocket->write(m_pSend, 13+2*regCnt);
    if (ret == -1)
    {
        KG_LOG_INFO("发送失败");
        return 0;
    }
    QString str = "发: ";
    for (int i=0; i<13+2*regCnt; i++)
    {
        str += QString("%1 ").arg((uchar)*(m_pSend+i),2,16,(QChar)'0');
    }
    str = str.toUpper();
    KG_LOG_INFO(QString("%1").arg(str));
    m_tcpSocket->waitForBytesWritten();
    m_tcpSocket->flush();
    int totalLength = 12;
    // 接收数据
    ret = m_tcpSocket->waitForReadyRead(1500);
    if (!ret)
    {
        KG_LOG_INFO("接收失败");
        return 0;
    }
    memset(m_pRecv,0,sizeof(m_pRecv));
    ret = m_tcpSocket->read(m_pRecv,sizeof(m_pRecv));
    if (ret == totalLength)
    {
        str = "收: ";
        for (int i=0; i<totalLength; i++)
        {
            str += QString("%1 ").arg((uchar)*(m_pRecv+i),2,16,(QChar)'0');
        }
        str = str.toUpper();
        KG_LOG_INFO(QString("%1").arg(str));      
        if((m_pRecv[0] == m_pSend[0]) 
        && (m_pRecv[1] == m_pSend[1]) // 事务标识
        && (m_pRecv[2] == m_pSend[2])
        && (m_pRecv[3] == m_pSend[3]) // 协议标识
        && ((m_pRecv[4]*256+m_pRecv[5]) == 6)
        && (m_pRecv[6] == m_pSend[6]) // 地址
        && (m_pRecv[7] == m_pSend[7]) // 命令
        && (m_pRecv[8] == m_pSend[8])
        && (m_pRecv[9] == m_pSend[9])
        && (m_pRecv[10] == m_pSend[10])
        && (m_pRecv[11] == m_pSend[11]))        
        {
            return 1;
        }
    }
    else
    {
        KG_LOG_INFO(QString("接收应答超时=%1<%2").arg(ret).arg(totalLength));
    }
    return 0;
}

void EthThread::devConfig(int devType)
{ 
    QSettings set(DEV_PATH, QSettings::IniFormat);
    if (devType == 0) 
    {
        m_strLog = QString("../log/ETH-%1_BCMU%2.log").arg(m_ethIndex).arg(m_bcIndex+1);      
        m_serverIP = set.value(QString("BCMU-%1/IP").arg(m_bcIndex+1), ("192.168.1." + QString::number(21+m_bcIndex))).toString();
        m_serverPort = set.value(QString("BCMU-%1/Port").arg(m_bcIndex+1), 502).toInt();          
        m_uMbap = 0;            
        dev.m_uAddress = 1;
        dev.m_uFunc = 0;    
        dev.m_uDelayMs = set.value(QString("BCMU-%1/DelayMs").arg(m_bcIndex+1),50).toUInt();
        dev.m_uCommTimeout = set.value(QString("BCMU-%1/CommTimeout").arg(m_bcIndex+1),10).toUInt();  
        dev.m_uCommState = 1;
        dev.m_uCommLastState = 5;
        dev.m_Time = QDateTime::currentDateTime();     
        CBattBank *pBattBank = m_pRTDB->getBank(m_bcIndex); 
        pBattBank->usResetSOC = 0xFFFF; 
    }   
    else if (devType == 1)
    {
        m_strLog = QString("../log/ETH-%1_PCS%2.log").arg(m_ethIndex).arg(m_bcIndex+1);      
        m_serverIP = set.value(QString("PCS-%1/IP").arg(m_bcIndex+1), "192.168.1.10").toString();
        m_serverPort = set.value(QString("PCS-%1/Port").arg(m_bcIndex+1), 502).toInt();     
        m_uMbap = 0;           
        dev.m_uAddress = set.value(QString("PCS-%1/Address").arg(m_bcIndex+1), 0).toInt();
        dev.m_uFunc = set.value(QString("PCS-%1/Function").arg(m_bcIndex+1), 0).toInt();    
        dev.m_uDelayMs = set.value(QString("PCS-%1/DelayMs").arg(m_bcIndex+1), 500).toUInt();
        dev.m_uCommTimeout = set.value(QString("PCS-%1/CommTimeout").arg(m_bcIndex+1), 10).toUInt();  
        dev.m_uCommState = 1;
        dev.m_uCommLastState = 5;
        dev.m_Time = QDateTime::currentDateTime();     
        // 数据置无效值
        CPcsData *pPcsData = m_pRTDB->getPcs(m_bcIndex);   
        pPcsData->m_uCommState = 0xFFFF;
        for (int j=0; j<95; j++) 
        {
            pPcsData->m_uRev[j] = 0xFFFF;		
        }
        pPcsData->m_uReset = 0xFFFF;
        pPcsData->m_uStart = 0xFFFF;
        pPcsData->m_uStop = 0xFFFF;
        pPcsData->m_uStandby = 0xFFFF;
        for (int j=0; j<98; j++) 
        {
            pPcsData->m_fRev[j] = FLT_MAX;	
        }    
        pPcsData->m_fSetYgPower = FLT_MAX;
        pPcsData->m_fSetWgPower = FLT_MAX;
    } 
    else if (devType == 2)
    {
        m_strLog = QString("../log/ETH-%1_UPS%2.log").arg(m_ethIndex).arg(m_bcIndex+1);      
        m_serverIP = set.value(QString("UPS-%1/IP").arg(m_bcIndex+1), "192.168.1.20").toString();
        m_serverPort = set.value(QString("UPS-%1/Port").arg(m_bcIndex+1), 502).toInt();     
        m_uMbap = 0;           
        dev.m_uAddress = set.value(QString("UPS-%1/Address").arg(m_bcIndex+1), 0).toInt();
        dev.m_uFunc = set.value(QString("UPS-%1/Function").arg(m_bcIndex+1), 0).toInt();    
        dev.m_uDelayMs = set.value(QString("UPS-%1/DelayMs").arg(m_bcIndex+1), 500).toUInt();
        dev.m_uCommTimeout = set.value(QString("UPS-%1/CommTimeout").arg(m_bcIndex+1), 10).toUInt();  
        dev.m_uCommState = 1;
        dev.m_uCommLastState = 5;
        dev.m_Time = QDateTime::currentDateTime();     
        // 数据置无效值
        CUpsData *pUpsData = m_pRTDB->getUpsDev(m_bcIndex);   
        pUpsData->m_uCommState = 0xFFFF;
        for (int j=0; j<98; j++) 
        {
            pUpsData->m_uRev[j] = 0xFFFF;		
        }
        for (int j=0; j<99; j++) 
        {
            pUpsData->m_fRev[j] = FLT_MAX;	
        }    
    } 
    else if (devType == 3)
    {
        m_strLog = QString("../log/ETH-%1_BMS%2.log").arg(m_ethIndex).arg(m_bcIndex+1);      
        m_serverIP = set.value(QString("BMS-%1/IP").arg(m_bcIndex+1), "192.168.1.20").toString();
        m_serverPort = set.value(QString("BMS-%1/Port").arg(m_bcIndex+1), 502).toInt();     
        m_uMbap = 0;           
        dev.m_uAddress = set.value(QString("BMS-%1/Address").arg(m_bcIndex+1), 0).toInt();
        dev.m_uFunc = set.value(QString("BMS-%1/Function").arg(m_bcIndex+1), 0).toInt();    
        dev.m_uDelayMs = set.value(QString("BMS-%1/DelayMs").arg(m_bcIndex+1), 500).toUInt();
        dev.m_uCommTimeout = set.value(QString("BMS-%1/CommTimeout").arg(m_bcIndex+1), 10).toUInt();  
        dev.m_uCommState = 1;
        dev.m_uCommLastState = 5;
        dev.m_Time = QDateTime::currentDateTime();     
    }
    m_isOutputLog = set.value("Log/IsOutputLog", 0).toInt();
    if (m_isOutputLog == 1)
    {
        QDir dir;
        dir.mkdir("../log");
        QString   logFileName = QString(m_strLog) ;
        QFile file(logFileName);
        file.remove();
        m_logFile.createlogfile(logFileName);
    }       
    if (devType == 1)
    {
        KG_LOG_INFO(QString("PCS%1 协议号=%2").arg(m_bcIndex+1).arg(dev.m_uFunc));
    }
    else if (devType == 2)
    {
        KG_LOG_INFO(QString("UPS%1 协议号=%2").arg(m_bcIndex+1).arg(dev.m_uFunc));
    }
    else if (devType == 3)
    {
        KG_LOG_INFO(QString("BMS%1 协议号=%2").arg(m_bcIndex+1).arg(dev.m_uFunc));
    }
}

void EthThread::writeKgLogfile(QString strInfo,bool bSaveToFile)
{
    QByteArray dt = strInfo.toLocal8Bit();
    char *p = dt.data();
    printf(p);
    printf("\n");
    if (1 != m_isOutputLog)//|| !bSaveToFile
    {
        return;
    }
    if (!bSaveToFile)
    {
        return;
    }
    int fileSize = m_logFile.fileSize();
    if (fileSize >= KG_MAX_LOG_SIZE)
    {
        printf("log size:%d, too large to delete\n", fileSize);
        m_logFile.close();
        QString   logFileName = QString(m_strLog);
        QFile file(logFileName);
        file.remove();
        m_logFile.createlogfile(logFileName);
    }    
    m_logFile.writelogfile(strInfo);
}
